//
//  XHBuyCouponsViewController.m
//  umbs
//
//  Created by 冷山 on 16/6/16.
//  Copyright © 2016年 冷山. All rights reserved.
//

#import "XHBuyCouponsViewController.h"

#import "XHBuyCouponsCell.h"
#import "XHBuyCpuponsDetailCell.h"

#import "Services.h"
#import "XHTabBarController.h"
#import "AppDelegate.h"

#import <UIAlertView-Blocks/UIAlertView+Blocks.h>

@interface XHBuyCouponsViewController ()

@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (weak, nonatomic) IBOutlet UIButton *ui_buyButton;
@property (strong, nonatomic) NSDictionary *m_userInfoDic;

@end

@implementation XHBuyCouponsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.ui_buyButton.layer.masksToBounds = YES;
    self.ui_buyButton.layer.cornerRadius = 8;
    [self.ui_buyButton setTitle:[NSString stringWithFormat:@"立即支付%@积分",self.m_couponsDic[@"salesPrice"]] forState:UIControlStateNormal];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHBuyCouponsCell" bundle:nil] forCellReuseIdentifier:XHBuyCouponsCellIdentifier];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHBuyCpuponsDetailCell" bundle:nil] forCellReuseIdentifier:XHBuyCpuponsDetailCellIdentifier];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getUserInfo];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)getUserInfo
{
    WS(weakSelf)
    self.m_userInfoDic=[ServiceManager getUserInfoSuccess:^(NSDictionary *data) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            weakSelf.m_userInfoDic = data;
        }
    } failure:^{
        
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger number = 2;
    return number;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger rows = 1;
    
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = nil;
    if (indexPath.section==0)
    {
        cell = [self buyCouponsCell:indexPath];
    }
    else if (indexPath.section==1)
    {
        cell = [self buyCpuponsDetailCell:indexPath];
    }
    
    return cell;
}

- (XHBuyCouponsCell*)buyCouponsCell:(NSIndexPath*)indexPath
{
    XHBuyCouponsCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHBuyCouponsCellIdentifier];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.ui_integral.text = [NSString stringWithFormat:@"%@",self.m_couponsDic[@"salesPrice"]];
    if ([self.m_couponsDic[@"operatorCode"]isEqualToString:@"10086"])
    {
        cell.ui_imageView.image = [UIImage imageNamed:@"shouye_icon_yidong"];
    }
    else if ([self.m_couponsDic[@"operatorCode"]isEqualToString:@"10010"])
    {
        cell.ui_imageView.image = [UIImage imageNamed:@"shouye_icon_liantong"];
    }
    else if ([self.m_couponsDic[@"operatorCode"]isEqualToString:@"10000"])
    {
        cell.ui_imageView.image = [UIImage imageNamed:@"shouye_icon_dianxin"];
    }
    
    
    return cell;
}

- (XHBuyCpuponsDetailCell*)buyCpuponsDetailCell:(NSIndexPath*)indexPath
{
    XHBuyCpuponsDetailCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHBuyCpuponsDetailCellIdentifier];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.ui_date.text = [[NSDate dateWithString:self.m_couponsDic[@"recDate"]] stringWithFormatter];
    NSString * startTime = [[NSDate dateWithString:self.m_couponsDic[@"startTime"]] stringSlashWithFormatter];
    NSString * endTime = [[NSDate dateWithString:self.m_couponsDic[@"endTime"]] stringSlashWithFormatter];
    cell.ui_usefulLife.text = [NSString stringWithFormat:@"%@ - %@",startTime,endTime];
    cell.ui_price.text = [NSString stringWithFormat:@"%@积分",self.m_couponsDic[@"officalPrice"]];
    cell.ui_initialAmount.text = [NSString stringWithFormat:@"%@元",self.m_couponsDic[@"minOrderAmount"]];
    if ([self.m_couponsDic[@"operatorCode"]isEqualToString:@"10086"])
    {
        //移动
        if ([self.m_couponsDic[@"suitableType"] isEqualToString:@"CALL"]||
            [self.m_couponsDic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
        {
            //话费
            if([self.m_couponsDic[@"couponType"] isEqualToString:@"VOUCHER_TICKET"]&&
               [self.m_couponsDic[@"suitableType"] isEqualToString:@"CALL"])
            {
                cell.ui_goodsType.text = @"话费直充代金券";
                cell.ui_discountLabel.text = @"抵扣金额";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@元",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"VOUCHER_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
            {
                cell.ui_goodsType.text = @"智能缴费代金券";
                cell.ui_discountLabel.text = @"抵扣金额";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@元",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"DISCOUNT_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"CALL"])
            {
                cell.ui_goodsType.text = @"话费直充抵扣券";
                cell.ui_discountLabel.text = @"折扣";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@折",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"DISCOUNT_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
            {
                cell.ui_goodsType.text = @"智能缴费抵扣券";
                cell.ui_discountLabel.text = @"折扣";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@折",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }

        }
        else if ([self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOW"]||
                 [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
        {
            //流量
            if([self.m_couponsDic[@"couponType"] isEqualToString:@"VOUCHER_TICKET"]&&
               [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOW"])
            {
                cell.ui_goodsType.text = @"流量直充代金券";
                cell.ui_discountLabel.text = @"抵扣金额";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@元",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"VOUCHER_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
            {
                cell.ui_goodsType.text = @"流量屯包代金券";
                cell.ui_discountLabel.text = @"抵扣金额";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@元",[self.m_couponsDic[@"couponValue"] roundFloorString]];

            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"DISCOUNT_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOW"])
            {
                cell.ui_goodsType.text = @"流量直充抵扣券";
                cell.ui_discountLabel.text = @"折扣";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@折",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"DISCOUNT_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
            {
                cell.ui_goodsType.text = @"流量屯包抵扣券";
                cell.ui_discountLabel.text = @"折扣";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@折",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
        }

    }
    else if ([self.m_couponsDic[@"operatorCode"]isEqualToString:@"10010"])
    {
        //联通
        if ([self.m_couponsDic[@"suitableType"] isEqualToString:@"CALL"]||
            [self.m_couponsDic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
        {
            //话费
            if([self.m_couponsDic[@"couponType"] isEqualToString:@"VOUCHER_TICKET"]&&
               [self.m_couponsDic[@"suitableType"] isEqualToString:@"CALL"])
            {
                cell.ui_goodsType.text = @"话费直充代金券";
                cell.ui_discountLabel.text = @"抵扣金额";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@元",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"VOUCHER_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
            {
                cell.ui_goodsType.text = @"智能缴费代金券";
                cell.ui_discountLabel.text = @"抵扣金额";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@元",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"DISCOUNT_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"CALL"])
            {
                cell.ui_goodsType.text = @"话费直充抵扣券";
                cell.ui_discountLabel.text = @"折扣";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@折",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"DISCOUNT_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
            {
                cell.ui_goodsType.text = @"智能缴费抵扣券";
                cell.ui_discountLabel.text = @"折扣";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@折",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
        }
        else if ([self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOW"]||
                 [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
        {
            //流量
            if([self.m_couponsDic[@"couponType"] isEqualToString:@"VOUCHER_TICKET"]&&
               [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOW"])
            {
                cell.ui_goodsType.text = @"流量直充代金券";
                cell.ui_discountLabel.text = @"抵扣金额";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@元",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"VOUCHER_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
            {
                cell.ui_goodsType.text = @"流量屯包代金券";
                cell.ui_discountLabel.text = @"抵扣金额";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@元",[self.m_couponsDic[@"couponValue"] roundFloorString]];
                
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"DISCOUNT_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOW"])
            {
                cell.ui_goodsType.text = @"流量直充抵扣券";
                cell.ui_discountLabel.text = @"折扣";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@折",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"DISCOUNT_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
            {
                cell.ui_goodsType.text = @"流量屯包抵扣券";
                cell.ui_discountLabel.text = @"折扣";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@折",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
        }

    }
    else if ([self.m_couponsDic[@"operatorCode"]isEqualToString:@"10000"])
    {
        //电信
        if ([self.m_couponsDic[@"suitableType"] isEqualToString:@"CALL"]||
            [self.m_couponsDic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
        {
            //话费
            if([self.m_couponsDic[@"couponType"] isEqualToString:@"VOUCHER_TICKET"]&&
               [self.m_couponsDic[@"suitableType"] isEqualToString:@"CALL"])
            {
                cell.ui_goodsType.text = @"话费直充代金券";
                cell.ui_discountLabel.text = @"抵扣金额";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@元",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"VOUCHER_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
            {
                cell.ui_goodsType.text = @"智能缴费代金券";
                cell.ui_discountLabel.text = @"抵扣金额";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@元",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"DISCOUNT_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"CALL"])
            {
                cell.ui_goodsType.text = @"话费直充抵扣券";
                cell.ui_discountLabel.text = @"折扣";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@折",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"DISCOUNT_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
            {
                cell.ui_goodsType.text = @"智能缴费抵扣券";
                cell.ui_discountLabel.text = @"折扣";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@折",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
        }
        else if ([self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOW"]||
                 [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
        {
            //流量
            if([self.m_couponsDic[@"couponType"] isEqualToString:@"VOUCHER_TICKET"]&&
               [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOW"])
            {
                cell.ui_goodsType.text = @"流量直充代金券";
                cell.ui_discountLabel.text = @"抵扣金额";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@元",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"VOUCHER_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
            {
                cell.ui_goodsType.text = @"流量屯包代金券";
                cell.ui_discountLabel.text = @"抵扣金额";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@元",[self.m_couponsDic[@"couponValue"] roundFloorString]];
                
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"DISCOUNT_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOW"])
            {
                cell.ui_goodsType.text = @"流量直充抵扣券";
                cell.ui_discountLabel.text = @"折扣";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@折",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
            else if ([self.m_couponsDic[@"couponType"] isEqualToString:@"DISCOUNT_TICKET"]&&
                     [self.m_couponsDic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
            {
                cell.ui_goodsType.text = @"流量屯包抵扣券";
                cell.ui_discountLabel.text = @"折扣";
                cell.ui_discount.text = [NSString stringWithFormat:@"%@折",[self.m_couponsDic[@"couponValue"] roundFloorString]];
            }
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0;
    if (section == 0)
    {
        height = 0.1;
    }
    else
    {
        height = 5;
    }
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0;
    if (indexPath.section==0)
    {
        height = 121;
    }
    else if (indexPath.section==1)
    {
        height = 213;
    }
    return  height;
}

- (IBAction)payAction:(id)sender
{
    if (self.m_userInfoDic) {
        long myScore=[self.m_userInfoDic[@"score"] longValue];
        long willPay=[self.m_couponsDic[@"salesPrice"] longValue];
        if (willPay>myScore) {
            [PromptAction showInfoPrompt:@"您的积分不足"];
            return;
        }
    }else{
        [PromptAction showInfoPrompt:@"亲~网络不好哦~"];
        return;
    }
    
    UIAlertView *alertView=[[UIAlertView alloc] init];
    alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
    alertView.title=@"请输入密码";
    alertView.message = @"登录密码即支付密码";
    UITextField *textField=[alertView textFieldAtIndex:0];
    textField.text=@"";
    textField.textAlignment = NSTextAlignmentCenter;
    [textField setSecureTextEntry:YES];
    RIButtonItem *cancelItem=[RIButtonItem itemWithLabel:@"取消"];
    RIButtonItem *okItem=[RIButtonItem itemWithLabel:@"确定" action:^{
        
        if ([NSString checkNullString:textField.text])
        {
            [PromptAction showInfoPrompt:@"请输入密码"];
        }
        else
        {
            [PromptAction showWaitPrompt:@"正在支付..."];
            [ServiceManager userBuyCouponsWithPassWord:textField.text amount:self.m_couponsDic[@"salesPrice"] marketSeq:self.m_couponsDic[@"sequenceNBR"] success:^(NSDictionary *data) {
                [PromptAction dismissPrompt];
                AppDelegate * delegate = [UIApplication sharedApplication].delegate;
                [delegate.tabBarController goMyCouponView];
                [self.navigationController popToRootViewControllerAnimated:YES];
            } failure:^(NSDictionary *data) {
//                [PromptAction dismissPrompt];
            }];
        }
    }];
    [alertView addButtonItem:cancelItem];
    [alertView addButtonItem:okItem];
    [alertView show];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
